/*
 * @Author: xiaozhuang-c-language 3535851673@qq.com
 * @Date: 2025-07-12 21:08:36
 * @LastEditors: xiaozhuang-c-language 3535851673@qq.com
 * @LastEditTime: 2025-07-12 22:04:52
 * @FilePath: /zhuang/BlogSystem/SynAsyLogSystem/example/Factory.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<memory>
using namespace std;


class Fruit
{
public:
   virtual void name() = 0;    
};

class Apple : public Fruit
{
public:
   void name()override
   {
     cout << "I am an Apple.." << endl;
   }    
};

class Banana : public Fruit
{
public:
    void name() override
    {
      cout << "I am a banana.." << endl;
    }    
};

/* 简单工厂模式 */
// class FruitFactory
// {
// public:
//    static std::shared_ptr<Fruit> CreateFruit(const string& product)
//    {
//        if(product == "苹果")
//           return make_shared<Apple>();
//        else
//          return make_shared<Banana>(); 
//    } 

// };

/* 工厂方法模式 */
// class FruitFactory
// {
// public:
//     virtual std::shared_ptr<Fruit> create() = 0; 
// };

// class AppleFactory : public FruitFactory
// {
// public:
//      std::shared_ptr<Fruit> create() override
//      {
//          return make_shared<Apple>();
//      }     
// };

// class BananaFactory : public FruitFactory
// {
// public: 
//     std::shared_ptr<Fruit> create() override
//      {
//          return make_shared<Banana>();
//      }  
// };

/* 抽象工厂模式 */
class Animal
{
public:
  virtual void name() = 0 ;    
};

class Dog : public Animal
{
public:
   void name() override
   {
     cout << "I am a dog" << endl;
   }    
};

class Cat : public Animal
{
public:
   void name() override
   {
     cout << "I am a cat" << endl;
   }  
};

class Factory
{
public:
   virtual std::shared_ptr<Fruit> getFruit(const string& fruit) = 0;
   virtual std::shared_ptr<Animal> getAnimal(const string& animal) = 0;
};

class FruitFactory : public Factory
{
public:
   virtual std::shared_ptr<Fruit> getFruit(const string& fruit)
   {
        if(fruit == "苹果") return make_shared<Apple>();
        else return make_shared<Banana>();
   }
   virtual std::shared_ptr<Animal> getAnimal(const string& animal){return shared_ptr<Animal>();}
};

class AniamlFactory : public Factory
{
public:
   virtual std::shared_ptr<Fruit> getFruit(const string& fruit)
   {
        return shared_ptr<Fruit>();
   }
   virtual std::shared_ptr<Animal> getAnimal(const string& animal)
   {
       if(animal == "小狗") return make_shared<Dog>();
       else return make_shared<Cat>();
   }
};




int main()
{
    //生产水果：
    shared_ptr<Factory> ff(new FruitFactory());
    shared_ptr<Fruit> fruit = ff->getFruit("苹果");
    fruit->name();
    //生产动物
    shared_ptr<Factory> af(new AniamlFactory());
    shared_ptr<Animal> animal = af->getAnimal("小狗");
    animal->name();
    return 0;
}


// int main()
// {
//     shared_ptr<FruitFactory> ff(new AppleFactory());
//     shared_ptr<Fruit> f = ff->create();
//     f->name();
//     ff.reset(new BananaFactory());  //reset方法提供了重新绑定指针的方式,同时释放原有指针指向的资源
//     f = ff->create();
//     f->name();
//     return 0;
// }



// int main()
// {
//     shared_ptr<Fruit> f = FruitFactory::CreateFruit("苹果");
//     f->name();
//     f = FruitFactory::CreateFruit("香蕉");
//     f->name();
//     return 0;
// }